package Date;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

/**
 * 演示作业
 * @author Administrator
 *
 */
public class PracticeDemo {

    /**
     * 上个月第一天是周几
     */
    @Test
    public void demo1() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DATE, 1);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1; // 天是从周天开始的
        System.out.println(dayOfWeek);
    }

    /**
     * 下个月最后一天是周几
     */
    @Test
    public void demo2() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        int days = calendar.getActualMaximum(Calendar.DATE);
        calendar.set(Calendar.DATE, days);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1; // 天是从周天开始的
        System.out.println(dayOfWeek);
    }

    /**
     * 上个月月初，获取上月第一天开始，形如：2020-08-01 00:00:00
     */
    @Test
    public void demo3() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),1, 0, 0, 0);
        Date date = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));//2020-07-01 00:00:00
    }

    /**
     * 下个月月末，最后一天
     */
    @Test
    public void demo4() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        int days = calendar.getActualMaximum(Calendar.DATE);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), days, 23, 59, 59);
        Date date = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
    }

    /**
     * 获取指定年的二月份天数
     */
    @Test
    public void demo5() {
        // 指定的年
        String yearString = "2021";
        int year = Integer.parseInt(yearString);

        // 计算
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
//		calendar.set(Calendar.MONTH, 1); // 月份是从零开始的
        calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
        int days = calendar.getActualMaximum(Calendar.DATE);
        System.out.println(days);

    }

    /**
     * 计算某一年母亲节是几号
     */
    @Test
    public void demo6() {
        // 指定的年
        String yearString = "2023";
        int year = Integer.parseInt(yearString);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, Calendar.MAY);
        // week_of_month  3
        // 第二个周的周末，每周是从周天的开始的，所以周数是3
        calendar.set(Calendar.WEEK_OF_MONTH, 3);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        Date date = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(simpleDateFormat.format(date));
    }
}
